#include "DiamondGame.h"
#include "Graphics.h"
#include "StateMgr.h"
#include "State.h"
#include "TimeController.h"
#include "TestState.h"
#include "Header.h"
#include "Utils.h"
#include "PlayState.h"
#include "time.h"
#include "stdlib.h"
#include "ResMgr.h"
#include "WinState.h"
#include "IntroState.h"
#include "MainMenuState.h"
#include "GameCommonInfo.h"

#ifdef ANDROID
#include "ApkLoader.h"
#endif

DiamondGame* DiamondGame::instance = 0;

DiamondGame::DiamondGame()
{
}
DiamondGame* DiamondGame::GetInstance()
{
	if(instance == 0)
	{
		PRINT("Init DiamondGame\n");
		instance = new DiamondGame();
	}
	return instance;
}
void DiamondGame::Init(int width, int height)
{
	TimeController* timeCtrl = TimeController::GetInstance();
	timeCtrl->SetFPSExpect(30);

	srand ( time(NULL) );

	Graphics* g = Graphics::GetInstance();
	g->Init(width, height);
	//ResMgr::GetInstance()->LoadFruitBox();
	//StateMgr::GetInstance()->SwitchState(PlayState::GetInstance());
	//StateMgr::GetInstance()->SwitchState(new IntroState());
	//StateMgr::GetInstance()->SwitchState(new TestState());
	StateMgr::GetInstance()->SwitchState(new IntroState());
}

void DiamondGame::Run()
{
	Update();
	Render();
}

void DiamondGame::Update()
{
	StateMgr::GetInstance()->Update();
	TimeController::GetInstance()->Limit();
	StateMgr::GetInstance()->GetCurrentState()->Update();
}

void DiamondGame::Render()
{
	glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
	glClearColor(1, 0, 0, 0);

	StateMgr::GetInstance()->GetCurrentState()->Render();

	Graphics* g = Graphics::GetInstance();
	g->RenderAll();
	
}

void DiamondGame::TouchUp(int x, int y)
{
	StateMgr::GetInstance()->GetCurrentState()->TouchUp(x, y);
}

void DiamondGame::TouchDown(int x, int y)
{
	StateMgr::GetInstance()->GetCurrentState()->TouchDown(x, y);
}

void DiamondGame::TouchMove(int x, int y)
{
	StateMgr::GetInstance()->GetCurrentState()->TouchMove(x, y);
}
void DiamondGame::Destroy()
{
	PRINT("Destroy DiamondGame\n");
	DestroyAllSingletonObject();
	ResMgr::GetInstance()->Destroy();
	GameCommonInfo::GetInstance()->DestroyInstance();
	DELETE_SAFELY(instance);
}

DiamondGame::~DiamondGame()
{
}

#ifdef ANDROID

extern "C" {
	JNIEXPORT void JNICALL Java_vng_kimcuong_NativeLib_init(JNIEnv * env, jobject obj,  jint width, jint height, jstring apkFileName);
	JNIEXPORT void JNICALL Java_vng_kimcuong_NativeLib_update(JNIEnv * env, jobject obj);
	JNIEXPORT void JNICALL Java_vng_kimcuong_NativeLib_render(JNIEnv * env, jobject obj);
	JNIEXPORT void JNICALL Java_vng_kimcuong_NativeLib_touchup(JNIEnv * env, jobject obj,  jint x, jint y);
	JNIEXPORT void JNICALL Java_vng_kimcuong_NativeLib_touchdown(JNIEnv * env, jobject obj,  jint x, jint y);
	JNIEXPORT void JNICALL Java_vng_kimcuong_NativeLib_touchmove(JNIEnv * env, jobject obj,  jint x, jint y);
	JNIEXPORT void JNICALL Java_vng_kimcuong_NativeLib_finish(JNIEnv * env, jobject obj);
};

JNIEXPORT void JNICALL Java_vng_kimcuong_NativeLib_init(JNIEnv * env, jobject obj,  jint width, jint height, jstring apkFileName)
{
	const char* str;
	jboolean isCopy;
	str = env->GetStringUTFChars(apkFileName, &isCopy);
	ApkLoader::GetInstance()->LoadAPK(str);
	ApkLoader::GetInstance()->SetApkFileName(str);
	DiamondGame::GetInstance()->Init(width, height);
}

JNIEXPORT void JNICALL Java_vng_kimcuong_NativeLib_update(JNIEnv * env, jobject obj)
{
	DiamondGame::GetInstance()->Update();
}

JNIEXPORT void JNICALL Java_vng_kimcuong_NativeLib_render(JNIEnv * env, jobject obj)
{
	DiamondGame::GetInstance()->Render();
}

JNIEXPORT void JNICALL Java_vng_kimcuong_NativeLib_touchup(JNIEnv * env, jobject obj,  jint x, jint y)
{
	DiamondGame::GetInstance()->TouchUp(x, y);
}

JNIEXPORT void JNICALL Java_vng_kimcuong_NativeLib_touchdown(JNIEnv * env, jobject obj,  jint x, jint y)
{
	DiamondGame::GetInstance()->TouchDown(x, y);
}

JNIEXPORT void JNICALL Java_vng_kimcuong_NativeLib_touchmove(JNIEnv * env, jobject obj,  jint x, jint y)
{
	DiamondGame::GetInstance()->TouchMove(x, y);
}

JNIEXPORT void JNICALL Java_vng_kimcuong_NativeLib_finish(JNIEnv * env, jobject obj)
{
	DiamondGame::GetInstance()->Destroy();
}
#endif